package com.example.webwork.controller;

import com.example.webwork.dto.AuthorArticleCount;
import com.example.webwork.entity.Article;
import com.example.webwork.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    @Qualifier("ArticleServiceImpl")
    private IArticleService articleService;

    @GetMapping("/author/{username}")
    public List<Article> getAllArticles(@PathVariable String username) {
        System.out.println(articleService.getAllArticles(username));
        return articleService.getAllArticles(username);
    }


    @GetMapping("/search")
    public List<Article> searchArticles(@RequestParam String username, @RequestParam String name) {
        System.out.println(articleService.searchArticles(username, name));
        return articleService.searchArticles(username, name);
    }

    @PostMapping("/add")
    public String addArticle(@RequestBody Article article) {
        articleService.addArticle(article);
        return "Article added successfully";
    }

    @PostMapping("/update")
    public String updateArticle(@RequestBody Article article) {
        articleService.updateArticle(article);
        return "Article updated successfully";
    }

    @DeleteMapping("/{id}")
    public String deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return "Article deleted successfully";
    }

    @GetMapping("/count/author")
    public List<AuthorArticleCount> getArticleCount() {
        List<AuthorArticleCount> counts=articleService.getArticleCounts();
        System.out.println(counts);
        return counts;
    }
}
